package com.thoughtworks.biblioteca.dao.file;

import com.thoughtworks.biblioteca.model.Menu;
import com.thoughtworks.biblioteca.model.MenuItem;
import com.thoughtworks.biblioteca.dao.MenuDao;

import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class MenuFileDao implements MenuDao {
    public Menu loadMenu() {
        Menu menu = new Menu();

        try {
            File f = new File("resources/menu.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(f)));
            String item;
            List<MenuItem> menuItems = new ArrayList<MenuItem>();
            while ((item = br.readLine()) != null) {
                String[] attr = item.split("[|]");
                int itemID = Integer.parseInt(attr[0].trim());
                String itemName = attr[1].trim();
                menuItems.add(new MenuItem(itemID, itemName));
            }
            menu.setMenuItems(menuItems);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return menu;
    }

}
